package cool.qingqing;


import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import cool.qingqing.bean.Day;
import cool.qingqing.service.MyBeanPost;
import cool.qingqing.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

@Configuration
//@ImportResource("classpath:jdbc.properties")
@ComponentScan(basePackages = "cool.qingqing")
public class Main {

/*	public static void main(String[] args) {
		// write your code here
		ApplicationContext bean = new ClassPathXmlApplicationContext("bean01.xml");
		UserService userService = (UserService) bean.getBean("userService");
		System.out.println("4.获取bean实例");
		userService.add();
		((ClassPathXmlApplicationContext) bean).close();
	}*/

	@Bean(initMethod = "init", destroyMethod = "end")
	public MyBeanPost beanPost() {
		return new MyBeanPost();
	}
	@Bean(name="dayAll")
	public Day getDay(){
		return new Day();
	}


/*	@Bean
	public DataSource dataSource() throws Exception {
		//1.从类路径下加载配置文件，获取一个输入流。如果不指定路径，默认是读取同一个包下资源文件
		InputStream inputStream = Main.class.getResourceAsStream("/jdbc.properties");
		//2.使用Properties对象的方法将配置文件中属性加载到Properties对象中
		Properties properties = new Properties();
		//加载了配置文件中所有的属性
		properties.load(inputStream);
		//3.通过druid的工厂类创建连接池
		DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

		//获取10个连接对象
		for (int i = 1; i <= 11; i++) {
			Connection connection = dataSource.getConnection();
			System.out.println("第" + i + "个连接对象：" + connection);
			//第3个连接关闭
			if (i == 3) {
				connection.close();
			}
		}


		return dataSource;
	}*/
}